﻿@using NewLife;
@using NewLife.Web;
@using XCode;
@using XCode.Configuration;
@{
    var entity = Model as IEntity;

    var page = ViewBag.Page as Pager;
    var fields = ViewBag.Fields as FieldCollection;
    var set = ViewBag.PageSetting as PageSetting ?? PageSetting.Global;
    var digits = new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
}
<tr>
    @if (set.EnableSelect)
    {
        <td></td>
    }
    @foreach (var item in fields)
    {
        if (item.PrimaryKey)
        {
            <td class="text-center">总计</td>
        }
        else
        {
            // 第二名称，去掉后面的数字，便于模式匹配
            var name2 = item.Name.TrimEnd(digits);

            var listField = item as ListField;
            var value = entity[item.Name];
            var tdClass = listField.GetTextClass();
            switch (Type.GetTypeCode(item.Type))
            {
                case TypeCode.Boolean:
                case TypeCode.DateTime:
                    <td></td>
                    break;
                case TypeCode.Decimal:
                    if (listField.GetValue != null)
                    {
                        <td class="@(tdClass ?? "text-center")">@listField.GetValue(entity)</td>
                    }
                    else if (item.ItemType.EqualIgnoreCase("Money"))
                    {
                        var dc = (Decimal)value;
                        <td class="@(tdClass ?? "text-right")">@dc.ToString("c")</td>
                    }
                    else
                    {
                        var dc = (Decimal)value;
                        if (item.Scale > 0)
                        {
                            <td class="@(tdClass ?? "text-right")">@dc.ToString("n" + item.Scale)</td>
                        }
                        else
                        {
                            <td class="@(tdClass ?? "text-right")">@dc.ToString()</td>
                        }
                    }
                    break;
                case TypeCode.Single:
                case TypeCode.Double:
                    if (listField.GetValue != null)
                    {
                        <td class="@(tdClass ?? "text-right")">@listField.GetValue(entity)</td>
                    }
                    else if (item.ItemType.EqualIgnoreCase("percent", "Percentage"))
                    {
                        var dd = (Double)value;
                        var scale = item.Scale >= 2 ? item.Scale - 2 : 2;
                        <td class="@(tdClass ?? "text-center")">@dd.ToString("p" + scale)</td>
                    }
                    else if (name2.EndsWith("Rate") || name2.EndsWith("Ratio") || item.ItemType.EqualIgnoreCase("percent", "Percentage"))
                    {
                        var dd = (Double)value;
                        var scale = 0;
                        var des = item.Description + "";
                        if (des.Contains("十分之一")) { dd /= 10; scale += 1; }
                        if (des.Contains("百分之一")) { dd /= 100; scale += 2; }
                        if (des.Contains("千分之一")) { dd /= 1000; scale += 3; }
                        if (des.Contains("万分之一")) { dd /= 10000; scale += 4; }
                        scale = scale >= 2 ? scale - 2 : 2;

                        <td class="@(tdClass ?? "text-center")">@dd.ToString("p" + scale)</td>
                    }
                    else
                    {
                        var dd = (Double)value;
                        if (item.Scale > 0)
                        {
                            <td class="@(tdClass ?? "text-right")">@dd.ToString("n" + item.Scale)</td>
                        }
                        else
                        {
                            <td class="@(tdClass ?? "text-right")">@dd.ToString()</td>
                        }
                    }
                    break;
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:
                    if (listField.GetValue != null)
                    {
                        <td class="@(tdClass ?? "text-right")">@listField.GetValue(entity)</td>
                    }
                    //特殊处理枚举
                    if (item.Type.IsEnum)
                    {
                        <td></td>
                    }
                    else if (item.ItemType.EqualIgnoreCase("TimeSpan"))
                    {
                        <td class="@(tdClass ?? "text-right")">@(TimeSpan.FromSeconds(value.ToInt()))</td>
                    }
                    else if (item.ItemType.EqualIgnoreCase("Time"))
                    {
                        <td class="@(tdClass ?? "text-center")">@(value.ToLong().ToDateTime().ToLocalTime().ToFullString(""))</td>
                    }
                    else if (item.ItemType.EqualIgnoreCase("GMK"))
                    {
                        <td class="@(tdClass ?? "text-right")">@(value.ToLong().ToGMK())</td>
                    }
                    else if (name2.EqualIgnoreCase("CreateUserID", "UpdateUserID") || name2.EndsWith("ID") || name2.EndsWith("Id"))
                    {
                        <td></td>
                    }
                    else if (name2.EndsWith("Rate") || name2.EndsWith("Ratio") || item.ItemType.EqualIgnoreCase("percent", "Percentage"))
                    {
                        var dd = Convert.ToDouble(value);
                        var scale = 0;
                        var des = item.Description + "";
                        if (des.Contains("十分之一")) { dd /= 10; scale = 0; }
                        if (des.Contains("百分之一")) { dd /= 100; scale = 0; }
                        if (des.Contains("千分之一")) { dd /= 1000; scale = 1; }
                        if (des.Contains("万分之一")) { dd /= 10000; scale = 2; }

                        <td class="@(tdClass ?? "text-center")">@dd.ToString("p" + scale)</td>
                    }
                    else
                    {
                        <td class="@(tdClass ?? "text-right")">@Convert.ToInt64(value).ToString("n0")</td>
                    }
                    break;
                case TypeCode.String:
                default:
                    <td></td>
                    break;
            }
        }
    }
    @if (this.Has(PermissionFlags.Detail, PermissionFlags.Update, PermissionFlags.Delete))
    {
        <td></td>
    }
</tr>